<?php

namespace App\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use Hash;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class AuthController extends Controller
{
    public function register(Request $request, User $user)
    {
        $request->validate([
            'mobile' => ['required', Rule::unique('users', 'mobile')],
            'password' => ['required', 'confirmed', 'between:5,20'],
            'captcha.content' => ['required', 'captcha_api:' . request('captcha.key') . ',flat']
        ], [
            'mobile.required' => '帐号不能为空',
            'mobile.unique' => '帐号已经被注册',
            'password.required' => '密码不能为空',
            'captcha.content.required' => '验证码不能为空',
            'captcha.content.captcha_api' => '验证码输入错误'
        ]);

        $user->fill($request->input());
        $user->password = Hash::make($request->password);
        $user->save();

        return $this->message('注册成功', ['password' => $user->password]);
    }
}
